/**
 * Generali Solutions d'assurances - Tous droits reserves &copy; 2007 - 2010
 */
package fr.generali.accueilclient.xref.services.impl;

import javax.xml.ws.WebServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import fr.generali.accueilclient.client.om.IListeIdentifiantsXReference;
import fr.generali.accueilclient.commun.exception.AccueilClientBusinessException;
import fr.generali.accueilclient.commun.exception.AccueilClientTechnicalException;
import fr.generali.accueilclient.xref.assembler.ConsultationXReferenceMapper;
import fr.generali.accueilclient.xref.services.IConsultationXReferenceService;
import fr.generali.refservcommuns.rce.consultationxref.consultationxrefservice.ConsulterXref;
import fr.generali.refservcommuns.rce.consultationxref.consultationxrefservice.ConsulterXrefResponse;
import fr.generali.refservcommuns.rce.consultationxref.contextesecurite.ContexteSecurite;
import fr.generali.refservcommuns.rce.consultationxref.services.IConsultationXRefWebService;
import fr.generali.refservcommuns.rce.consultationxref.services.TechnicalFault;
import fr.generali.refservcommuns.rce.consultationxref.services.WsRceBusinessException;

/**
 * The Class ConsultationXReferenceService.
 * 
 * @author Holmes Kelly
 */

@Service("consultationXReferenceService")
public class ConsultationXReferenceService implements IConsultationXReferenceService {

    /** LOGGER. */
    private static final Logger LOGGER = LoggerFactory.getLogger(ConsultationXReferenceService.class);

    /** The consultation x ref web service. */
    @Autowired
    @Qualifier("P61")
    private IConsultationXRefWebService consultationXRefWebService;

    /** The consultation x reference mapper. */
    @Autowired
    @Qualifier("consultationXReferenceMapper")
    private ConsultationXReferenceMapper consultationXReferenceMapper;

    /**
     * {@inheritDoc}
     */
    @Override
    public IListeIdentifiantsXReference consulterReferences(IListeIdentifiantsXReference identifiantsXReference)
                    throws AccueilClientBusinessException {

        IListeIdentifiantsXReference identifiantsXReferenceLocal = null;

        if (identifiantsXReference != null) {

            try {
                ConsulterXref parametersRequest = new ConsulterXref();

                parametersRequest.setDtoListeXRef(consultationXReferenceMapper
                                .mapperListeIdentifiantsXReference2DtoListeXRef(identifiantsXReference));
                parametersRequest.setContexteSecurite(new ContexteSecurite());

                ConsulterXrefResponse consulterXrefResponse =
                                consultationXRefWebService.consulterXref(parametersRequest);

                identifiantsXReferenceLocal =
                                consultationXReferenceMapper
                                                .mapperDtoListeXRef2IListeIdentifiantsXReference(consulterXrefResponse
                                                                .getDtoListeXRef());

            } catch (WsRceBusinessException e) {
                LOGGER.debug(e.getMessage());
                throw new AccueilClientBusinessException(e.getMessage(), e);
            } catch (TechnicalFault e) {
                LOGGER.debug(e.getMessage());
                throw new AccueilClientTechnicalException(e.getMessage());
            } catch (WebServiceException e) {
                LOGGER.debug(e.getMessage());
                throw new AccueilClientTechnicalException(e.getMessage());
            }

        }

        return identifiantsXReferenceLocal;
    }
}
